/*************************************************************************
 * File Name:    Combinations.cc
 * Author:       zero91
 * Mail:         jianzhang9102@gmail.com
 * Created Time: 2013/11/7 23:56:51
 * 
 * Description:  
 |------------------------------------------------------------------------
 | Problem: Combinations
 | Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
 |
 | For example,
 | If n = 4 and k = 2, a solution is:
 |
 | [
 |   [2,4],
 |   [3,4],
 |   [2,3],
 |   [1,2],
 |   [1,3],
 |   [1,4],
 | ]
 |------------------------------------------------------------------------
 ************************************************************************/

#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <map>
#include <set>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>

using namespace std;

class Solution {
public:
    vector<vector<int> > combine(int n, int k)
    {
        vector<vector<int> > ans;
        vector<int> num;
        
        dfs(ans, num, k, n);
        
        return ans;
    }
    
private:
    void dfs(vector<vector<int> > &ans, vector<int> &num, int k, int n)
    {
        if (k == 0) {
            reverse(num.begin(), num.end());
            ans.push_back(num);
            reverse(num.begin(), num.end());
            return;
        }
        for (int i = k; i <= n; ++i) {
            num.push_back(i);
            dfs(ans, num, k - 1, i - 1);
            num.pop_back();
        }
    }
};

